/*
 * 
 */
package messageAPI;

import messageAPI.Core.IDistributedCommunicationHandler;
import messageAPI.Core.IPerformanceInstrumentationService;
import messageAPI.Core.PerformanceEvent;
import messageAPI.DistributedCommunication.CommunicationHandlerFactory;

import java.util.List;

/**
 * The Class PerformanceInstrumentationService. This object is used to publish events to the DishDash API
 */
class PerformanceInstrumentationService implements IPerformanceInstrumentationService {
	   
   	/** The instance. */
   	private static PerformanceInstrumentationService instance = null;
    private IDistributedCommunicationHandler distributedCommunicationHandler;

   	/**
   	 * Instantiates a new performance instrumentation service.
   	 */
   	protected PerformanceInstrumentationService() {
	      // Exists only to defeat instantiation.
           try {
               distributedCommunicationHandler = CommunicationHandlerFactory.getDistributedCommunicationHandler();
           } catch (Throwable throwable) {
               throwable.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
           }
       }
	   
   	/**
   	 * Gets the single instance of PerformanceInstrumentationService.
   	 *
   	 * @return single instance of PerformanceInstrumentationService
   	 */
   	public static PerformanceInstrumentationService getInstance() {
	      if(instance == null) {
	         instance = new PerformanceInstrumentationService();
	      }
	      return instance;
	   }
	   
	   /* (non-Javadoc)
   	 * @see messageAPI.Core.IPerformanceInstrumentationService#sendEvent(messageAPI.Core.PerformanceEvent)
   	 */
//   	@Override
//		public void sendEvent(PerformanceEvent performanceEvent)
//	    {
//	    	this.send(performanceEvent);
//            this.distributedCommunicationHandler.shutDownHandler();
//	    }

    @Override
        public void sendEvents(List<PerformanceEvent> performanceEvents)
        {
             for(PerformanceEvent performanceEvent : performanceEvents)
             {
                 this.send(performanceEvent);
             }
            this.distributedCommunicationHandler.shutDownHandler();
        }

    private void send(PerformanceEvent performanceEvent)
    {
        try {
            distributedCommunicationHandler.sendMessage(performanceEvent.Category, performanceEvent.toString());
        } catch (Throwable e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}


// OLD CODE: MUST BE DELETED ONCE INTEGRATION TESTING IS SIGNED OFF
//
//class PerformanceInstrumentationService implements IPerformanceInstrumentationService {
//	
//    /* (non-Javadoc)
//	 * @see messageAPI.IInstrumentationHandler#CreateEntry(messageAPI.PerformanceEvent)
//	 */
//    @Override
//	public void sendEvent(PerformanceEvent performanceEvent)
//    {
//    	IDistributedCommunicationHandler distributedCommunicationHandler;
//		try {
//			distributedCommunicationHandler = new RabbitMQHandler();
//			distributedCommunicationHandler.SendMessage(performanceEvent.Category, performanceEvent.toString());
//		} catch (Throwable e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		
//    	
//    	
//    }
//}